package cn.myframe.config;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

/**
 * MVC拦截器配置
 * 
 * @author  ynz
 * @email   ynz@myframe.cn
 * @version 创建时间：2017年11月21日 下午12:27:49
 */
@Configuration
public class WebMvcConfig  implements WebMvcConfigurer {


    /**
     * 用法:registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html","/auth/**");
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**");*/
      //  registry.addInterceptor(new URLInterceptor()).addPathPatterns("/bus/**","/adv/**");
    }

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//将所有/static/** 访问都映射到classpath:/static/ 目录下
		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
	}

    
    
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {   
    	for(int i = 0; converters.size() > i ; i++){
    		if(converters.get(i) instanceof MappingJackson2HttpMessageConverter){
    			ObjectMapper objectMapper = new ObjectMapper();
    	        SimpleModule simpleModule = new SimpleModule();
    	        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    	        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
    	        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    	        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    	        objectMapper.registerModule(simpleModule);
    	        ((AbstractJackson2HttpMessageConverter) converters.get(i)).setObjectMapper(objectMapper);
    		}
    		
    	}
      /*  MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);*/     
    }
}
